<?php
/**
 *
 * @author fanzhanao
 * @version 
 */

include_once(APP_PATH."/cartoon/global.php");
class cartoon_model_chapter extends cartoon_model_bmodel
{
   /**
     * Constructor: initialize plugin loader
     * 
     * @return void
     */
    public function __construct(){
    	parent::__construct();
        $this->table = 'cartoon_chapter';
    }
    
	/**
	 * 添加漫画章节
	 * @param array $para=array(
	 * 		'cartoon_id'=>'漫画id'，
	 * 		'chapter_title'=>'漫画标题'，
	 * 		'pic_num'=>'包含图片数'，//默认为0
	 * 		'ordernum' => '章节排序'
	 * ) 
	 */
	public function add_chapter($para){
		if($para['cartoon_id'] && $para['chapter_title']){
			if($rs = $this->db->insert($this->table,$para)){
				$args['id'] = $para['cartoon_id'];
				$args['ct_last_chapterid']	=	$rs;
				$args['ct_last_chapter']	=	$para['ct_last_chapter'];		
				$this->modifyData($args,'cartoon_cartoon','id');	
				Model::factory('cartoon_model_data')->upChapterNum($para['cartoon_id']);
//				MemRow::incrList($rs,array($this,'getChapterListIds',array($para['cartoon_id'])));
				return $rs;
			}
		}
		return false;
	}
	
	/**
	 * 删除章节
	 *
	 * @param  int $chapter_id
	 * @return unknown
	 */
	public function del_chapter($chapter_id){
		if(!$chapter_id) return false;
		$chapter_id = is_array($chapter_id)?implode(',',$chapter_id):$chapter_id;
		$this->db->query("DELETE  FROM cartoon_images WHERE img_chapter_id IN ($chapter_id) ") ;
		return $this->db->query("DELETE  FROM $this->table WHERE id IN ($chapter_id) ");
		
		/*if(is_numeric($chapter_id)){
			if($rs = $this->db->insert($this->table,$para)){
				Model::factory('cartoon_model_data')->upChapterNum($para['cartoon_id']);
//				MemRow::decrList($rs,array($this,'getChapterListIds',array($para['cartoon_id'])));
//				MemRow::removeCache(CACHE_PRE_CHAPTER.$chapter_id);
				return $rs;
			}
		}
		return false;*/
	}
    
	/**
	 * 获取章节信息
	 * @param unknown_type $chapter_id
	 */
	public function getChapterRow($chapter_id){
		if(!is_numeric($chapter_id)) return false;
		return $this->db->fetchRow("SELECT * FROM $this->table WHERE id=$chapter_id");
	}
		
	public function getRows($chapter_id){
		$chapter_id = is_array($chapter_id)?implode(',',$chapter_id):$chapter_id;
		return $this->db->fetchAll("SELECT * FROM $this->table WHERE id IN ($chapter_id)");
	}
	
	/**
	 * 缓存获取章节信息
	 *
	 * @param unknown_type $chapter_id
	 * @param unknown_type $flush
	 */
	public function getChapterInfo($chapter_id,$flush = false){
		if(!is_numeric($chapter_id)) return false;
		$key = CACHE_PRE_CHAPTER.$chapter_id;
		if(!$info = MemRow::getCache($key) || $flush == true){
			$info = self::getChapterRow($chapter_id);
			MemRow::setCache($key,$info,0);
		}
		return $info;
	}
	
	/**
	 * 获取某个漫画章节列表
	 * @param unknown_type $cartoon_id
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getChapterListPages($cartoon_id,$page=1,$pageSize=15){
		if(!is_numeric($cartoon_id)) return false;
		$sql = "SELECT * FROM $this->table WHERE cartoon_id = $cartoon_id ORDER BY id DESC ";
		return $this->db->selectPage($sql,array(),$page,$pageSize);
	}
	
	/**
	 * 根据漫画id获取该漫画所有的章节数
	 *
	 * @param unknown_type $cartoon_id
	 * @return unknown
	 */
	public function getChapterList($cartoon_id,$num = 0){
		if(!is_numeric($cartoon_id)) return false;
		$sql = "SELECT * FROM $this->table WHERE cartoon_id = $cartoon_id  ";
		if($num>0){
			$sql.=' LIMIT '.$num;
		}
		return $this->db->fetchAll($sql);
	}
	
	
	/**
	 * 根据漫画id获取该漫画所有的章节数
	 *
	 * @param unknown_type $cartoon_id
	 * @return unknown
	 */
	public function getChapterListIds($cartoon_id){
		if(!is_numeric($cartoon_id)) return false;
		$return = array();
		if($ids = $this->db->fetchAll("SELECT id FROM $this->table WHERE cartoon_id = $cartoon_id  ")){
			while ($id = current($ids)){
				array_push($return,$id['id']);
				next($ids);
			}
		}
		return $return;
	}
/**
	 * 更新章节的图片数量
	 *
	 * @param unknown_type $chapter_id
	 */
	public function upChapterPhotoNum($chapter_id,$cartoon_id = null){
		if(!is_numeric($chapter_id)) return false;
		
		$cnt = $this->db->fetchField("SELECT COUNT(*) AS num FROM cartoon_images WHERE img_chapter_id = $chapter_id ".($cartoon_id?' AND img_cartoon_id='.$cartoon_id:''),'num');
		
		if($cnt){
			$this->db->query("UPDATE $this->table SET pic_num=$cnt,up_time=".CURRENT_TIME." WHERE id=$chapter_id ");
			//更新缓存
			$this->getChapterInfo($cartoon_id,true);
		}
		return true;
	}
	
	/**
	 * 设置漫画集的排行
	 * @param unknown_type $chapter_id
	 * @param unknown_type $order
	 */
	public function setRankChapter($chapter_id,$order){
		if(!(is_numeric($chapter_id) && $order)) return false;
		return $this->db->query("UPDATE $this->table SET order_num = $order WHERE id=$chapter_id");
	}
	
}